home *** CD-ROM | disk | FTP | other *** search
/ PsL Monthly 1993 December / PSL Monthly Shareware CD-ROM (December 1993).iso / prgmming / dos / basic / qbmath.exe / QBMATH1.BI < prev    next >
Encoding:
Text File  |  1993-05-03  |  10.3 KB  |  239 lines

  1. ' QBMATH1.LIB - Math library for Microsoft QuickBASIC
  2. '               writen in QuickBASIC 4.5
  3. '               by David Katelansky
  4. '                  9802 Forum Park
  5. '                  Apartment 3197
  6. '                  Houston, Tx.  77036
  7. '               registration fee is $10.00
  8. '
  9. '    Include File for QBMATH1.LIB
  10. '    always put the statement   'INCLUDE:'QBMATH1.BI'
  11. '    at the top of your programs when using this library
  12. '
  13. ' this math library can be run from the integrated environment by starting
  14. ' QuickBASIC with the following statement: QB/L QBMATH1
  15. '
  16. ' this math library can be linked into your programs by answering the
  17. ' library prompt when linking with QBMATH1.LIB (or QBMATH1+, if you have other
  18. ' libraries to follow)
  19. '
  20. DECLARE FUNCTION gcd& (a&, b&)   'greatest common denominator
  21.         ' syntax: greatest& = gcd&(a&, b&)
  22. '
  23. DECLARE FUNCTION primefact$ (z&) 'prime factors of an integer
  24.         ' syntax: answer$ = primefact$(z&)
  25.         ' the answer is a string with the integers separated by  ":" each
  26.         ' e.g., answer$ = primefact$(12) will result in answer$ becoming
  27.         ' the string 1: 2: 3: 4: 6:
  28. '
  29. DECLARE FUNCTION polyarea& (verts%, coord$)      ' area of a polygon
  30.         ' syntax: areaofpoly& = polyarea&(verts%, coord$)
  31.         ' verts% is an integer from 1 to 25 (number of vertices in polygon)
  32.         ' coord$ is a string that contains the polygon coordinates
  33.         ' coord$ has the form "x1,y1:x2,y2,x3,y3:"
  34.         ' for as many points as you want, as long as each point is
  35.         ' of the form x,y:
  36.         ' e.g.,  areaofpoly& = polyarea&(4,"0,0:10,0:10,10:0,10:")
  37.         ' will find the area of a square - 4 vertices, 10 units per side
  38.         ' vertex coordinates (0,0), (10,0), (10,10), (0,10)
  39.         ' with the resul being areaofpoly& being 100
  40. '
  41. TYPE Vectortype
  42.         xcoord AS INTEGER       ' x coordinate
  43.         ycoord AS INTEGER       ' y coordinate
  44.         zcoord AS INTEGER       ' z coordinate
  45. END TYPE
  46. TYPE VecInfo
  47.         magnitude AS INTEGER      ' magnitude of vector
  48.         xangle    AS INTEGER      ' angle with x-axis
  49.         yangle    AS INTEGER      ' angle with y-axis
  50.         zangle    AS INTEGER      ' angle with z-axis
  51.         inclangl  AS INTEGER      ' included angle, angle with other vector
  52. END TYPE
  53. DECLARE FUNCTION FAN! (x AS ANY)  ' called by the following subroutine
  54. ' gives relationship between two vectors - angles, magnitudes
  55. DECLARE SUB vectorelate (a AS Vectortype, b AS Vectortype, Ainfo AS VecInfo, Binfo AS VecInfo)
  56.         'syntax: Call vectorelate(A, B, Ainfo, Binfo)
  57.         ' Before calling this routine you must set the values for A and B
  58.         ' using the above types; vectortype and VecInfo.
  59.         ' You need the following definitions:
  60.         ' DIM A AS vectortype
  61.         ' DIM B AS vectortype
  62.         ' DIM Ainfo AS VecInfo
  63.         ' DIM Binfo AS VecInfo
  64.            ' right after the include statement ('$INCLUDE:'QBMATH.BI') in your
  65.            ' program header (i.e., before any executable statements)
  66.         ' example
  67.          'DIM A AS vectortype
  68.          'DIM B AS vectortype
  69.          'DIM Ainfo AS VecInfo
  70.          'DIM Binfo AS VecInfo
  71.          'A.xcoord = 3
  72.          'A.ycoord = 10
  73.          'A.zcoord = 0
  74.          'B.xcoord = 2
  75.          'B.ycoord = 5
  76.          'B.zcoord = 0
  77.          'CALL vectorelate(A, B, Ainfo, Binfo)
  78.          'PRINT Ainfo.magnitude
  79.          'PRINT Binfo.magnitude
  80.          'PRINT Ainfo.xangle, Ainfo.yangle, Ainfo.zangle, Ainfo.inclangl
  81.          'PRINT Binfo.xangle, Binfo.yangle, Binfo.zangle, Binfo.inclangl
  82.   ' with all answers in degrees   (Ainfo.inclangl = Binfo.inclangl, of course)
  83. '
  84. TYPE vecsymb
  85.         oper AS STRING * 1
  86. END TYPE
  87. ' vector operator - can select addition, subtraction, dot product, cross product
  88. DECLARE SUB vecoper (oper AS vecsymb, C AS Vectortype, b AS Vectortype, a AS Vectortype)
  89. '       syntax: call vecoper(vecsymb, C, B, A)
  90. '       result of opweration in C
  91. '       legal opoerations are:
  92. '       C = A + B       - vector addition
  93. '       C = A - B       - vector subtraction
  94. '       C = A . B       - vector dot product
  95. '       C = A * B       - vector cross product
  96. '       e.g.
  97. '       DIM vecsign AS vecsymb
  98. '       vecsign.oper = "*"
  99. '       A.xcoord = 1
  100. '       .
  101. '       .
  102. '       .
  103. '       CALL vecoper(vecsign, C, B, A)
  104. '       results in C.xcoord = A * B
  105. '       all other operations result in Cx, Cy, and Cz being set
  106. '       an illegal operation results in the vector C = (0,0,0)
  107. '
  108. TYPE tritype
  109.         tristr AS STRING * 3
  110.         ' legal entries for tristr are:
  111.         ' "ASA", "SAS", "AAS", "SSA" ,"SSS"
  112. END TYPE
  113. TYPE triangleinfo
  114.         solution AS INTEGER     ' -1 means no solution, 0 means solution OK
  115.         side1 AS INTEGER        ' length of side1
  116.         side2 AS INTEGER        ' length of side2
  117.         side3 AS INTEGER        ' length of side3
  118.         oppangl1 AS INTEGER     ' angle opposite side1 - in degrees
  119.         oppangl2 AS INTEGER     ' angle opposite side2 - in degrees
  120.         oppangl3 AS INTEGER     ' angle opposite side3 - in degrees
  121. END TYPE
  122. DECLARE FUNCTION TRIA (x AS ANY)        ' called by triangle
  123. DECLARE FUNCTION TRIB (x AS ANY)        ' called by triangle
  124. ' get all info on triangle if you know minimal info, listed in tritype
  125. DECLARE SUB triangle (descriptor AS tritype, val1%, val2%, val3%, trinfo AS triangleinfo)
  126.         ' syntax: CALL triangle(tri, val1%, val2%, val3%, trinfo)
  127.         ' example
  128.         ' DIM tri AS tritype
  129.         ' DIM trinfo AS triangleinfo
  130.         ' tri.tristr = "SSS"
  131.         ' CALL triangle(tri, 10, 10, 10, trinfo)
  132.         ' this would return the info for this triangle in trinfo
  133. '
  134. DECLARE SUB cart2pol (x, y, r, theta)     ' convert from cartesian coordinates
  135.                                           ' to polar coordinates
  136. '       syntax CALL cart2pol(x, y, r, theta, phi)
  137. '       you just have to supply the cartesian coordinates
  138. '       x, y. This routine returns:
  139. '       r - distance from origin of coordinate system
  140. '       theta - angle in degrees
  141. DECLARE SUB pol2cart (x, y, r, theta)           ' convert from polar
  142.                                                 ' to cartesian coordinates
  143. '       syntax CALL pol2cart(x, y, r, theta)
  144. '       you just supply the polar coordinates.
  145. '       this routine returns the cartesian coordinates
  146. '
  147. DECLARE FUNCTION deg2rad (DEGS, MINS, SECS) ' convert from degrees to radians
  148. '       syntax rads = deg2rad(degs, mins, secs)
  149. '       degs are degrees of arc
  150. '       mins are minutes of arc
  151. '       secs are seconds of arc
  152. '       these are all single precision, floating point numbers
  153. '       so degrees.fraction_of_degrees is OK
  154. '
  155. DECLARE SUB rad2deg (rad, DEGS, MINS, SECS)
  156. '       syntax CALL rad2deg(rad, degs, mins, secs)
  157. '       you supply the radians (rad) and the values
  158. '       degs (degrees), mins (minutes), and secs (seconds) are returned
  159. '
  160. DECLARE FUNCTION lininterpy (x1, y1, x2, y2, x) ' linear interpolation
  161.                 ' of y - you supply two points (x1,y1) and (x2,y2)
  162.                 ' and an x value - the function returns the corresponding
  163.                 ' y value
  164.                 ' syntax y = lininterpy(x1, y1, x2, y2, x)
  165. '
  166. DECLARE FUNCTION lininterpx (x1, y1, x2, y2, y) ' linear interpolation
  167.                 ' of x - you supply two points (x1,y1) and (x2,y2)
  168.                 ' and a y value - the function returns the corresponding
  169.                 ' x value
  170.                 ' syntax x = lininterpx(x1, y1, x2, y2, y)
  171. '
  172. TYPE complex                  ' complex number
  173.         real AS DOUBLE
  174.         compart AS DOUBLE
  175. END TYPE
  176. '
  177. DECLARE SUB complexadd (C AS complex, b AS complex, a AS complex)
  178. '       add two complex numbers
  179. '       syntax DIM C AS complex, B AS complex, A AS complex
  180. '       CALL complexadd(C, B, A)
  181. '       results in C = A + B
  182. '
  183. DECLARE SUB complexsub (C AS complex, b AS complex, a AS complex)
  184. '       subtract two complex numbers
  185. '       syntax DIM C AS complex, B AS complex, A AS complex
  186. '       CALL complexsub(C, B, A)
  187. '       results in C = A - B
  188. '
  189. DECLARE SUB complexmul (C AS complex, b AS complex, a AS complex)
  190. '       multiply two complex numbers
  191. '       syntax DIM C AS complex, B AS complex, A AS complex
  192. '       CALL complexmul(C, B, A)
  193. '       results in C = A * B
  194. '
  195. DECLARE SUB complexdiv (C AS complex, b AS complex, a AS complex)
  196. '       divide two complex numbers
  197. '       syntax DIM C AS complex, B AS complex, A AS complex
  198. '       CALL complexdiv(C, B, A)
  199. '       results in C = A / B
  200. '
  201. DECLARE SUB complexroot (root%, b AS complex, a AS complex)
  202. '       get root of complex number
  203. '       syntax DIM B AS complex, A AS complex
  204. '       CALL complexroot(root%, B, A)
  205. '       results in B.real = magnitude(A ^ root%)
  206. '               B.compart = B.real
  207. '       the actual root is B.real*(cos(theta/root%) + i*sin(theta/root%))
  208. '
  209. DECLARE SUB quadratic (a, b, C, x1 AS complex, x2 AS complex) ' get roots of quadratic equation
  210. '       syntax DIM x1 AS complex: DIM x2 as complex
  211. '       CALL quadratic(a, b, c, x1, x2)
  212. '       where you supply the coefficients a, b and c
  213. '       for the equation a * x^2 + b * x + c = 0
  214. '       real parts of roots are in real part of number
  215. '       e.g., real part of x1 = x1.real
  216. '       e.g., complex part of x1 = x1.compart
  217. '
  218. DECLARE SUB newton (degree%, root!, incs!, coord$)
  219. '       real roots of equation of degree degree%
  220. '       coefficients can range in value from -coeff! to +coeff!
  221. '       the newton algorithm will search in increments of incs!
  222. '       from -root! to +root! searching for roots to the equation
  223. '       syntax CALL newton(degree%, root, incs, coord$)
  224. '       where coord$ is of the same form as in polyarea
  225. '       e.g., to solve for the roots of  2 * x ^ 3 + 4 * x ^ 2 + x - 3 = 0
  226. '       coord$ = "2,3:4,2:1,1:-3,0:"
  227. '       degree% = 3     3rd degree equation
  228. '       root = 10       try roots from -10 to 10
  229. '       incs = 0.1
  230. '       CALL newton(degree%, root, incs, coord$)
  231. '       i.e., coord$ is of the form "coefficient, power:"
  232. '       NOTE: if the coefficient is zero you dont have to include it
  233. '       UNLESS THE POWER IS 0, THEN YOU MUST INCLUDE 0,0: AT THE END OF coord$
  234. '       otherwise you will get a run time error
  235. '       the results (the roots of the equation) are stored in coord$ and can
  236. '       be parsed out after the call is made (roots delimited by ",")
  237. '
  238.  
  239.